<template>
  <n-card :bordered="false" class="proCard">
    <BasicForm @register="register" @reset="handleReset" @submit="handleSubmit">
      <template #areaListSelect="{ model, field }">
        <n-select
          v-model:value="model[field]"
          :options="optionsRef"
          clearable
          filterable
          placeholder="选择区服"
        />
      </template>
      <template #gameSelect="{ model, field }">
        <n-input v-model:value="model[field]" :options="gameOptions" clearable />
      </template>
    </BasicForm>

    <BasicTable
      ref="actionRef"
      :columns="columns"
      :initRequest="false"
      :request="loadDataTable"
      :row-key="getRowKey"
      :scroll-x="1090"
      @update:checked-row-keys="onCheckedRow"
    >
      <template #toolbar>
        <n-button type="primary" @click="reloadTable">刷新数据</n-button>
      </template>
    </BasicTable>
  </n-card>
</template>

<script lang="ts" setup>
  import { nextTick, onMounted, reactive, ref, unref } from 'vue';
  import { useMessage } from 'naive-ui';
  import { BasicTable } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { columns } from './columns';
  import { useRouter } from 'vue-router';
  import { getPlayerRolePageList } from '@/api/player/playerRoleManage';
  import { getAllArea, getGameAll } from '@/api/game/game';

  const schemas: FormSchema[] = [
    {
      field: 'userId',
      component: 'NInput',
      label: '玩家ID',
      componentProps: {
        placeholder: '玩家ID',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'roleId',
      component: 'NInput',
      label: '角色ID',
      componentProps: {
        placeholder: '角色ID',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'roleName',
      component: 'NInput',
      label: '角色名',
      componentProps: {
        placeholder: '角色名',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'serverId',
      label: '区服',
      slot: 'areaListSelect',
    },
  ];
  const gameOptions = ref([]);
  const router = useRouter();
  const formRef: any = ref(null);
  const message = useMessage();
  const actionRef = ref();
  const loadingRef = ref(false);
  const optionsRef = ref([]);
  const formBtnLoading = ref(false);
  const formParams = reactive({
    gameKey: '',
    roleId: '',
    userId: 0,
    serverId: 0,
  });

  const params = ref({
    userId: 0,
  });

  const [register, { setFieldsValue }] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
    labelWidth: 120,
    schemas,
  });
  const getRowKey = (row) =>{
    return row.roleId;
  };
  const loadDataTable = async (res) => {
    if (!formParams.userId) {
      formParams.userId = 0;
    } else {
      formParams.userId = parseInt(formParams.userId);
    }
    if (!formParams.serverId) {
      formParams.serverId = 0;
    } else {
      formParams.serverId = parseInt(formParams.serverId);
    }
    const param = { ...formParams, ...res };
    let data = await getPlayerRolePageList(param);
    // if(data && data.list && data.list.length > 0)
    // {
    //   data.list = data.list.map((item) => {
    //     return {
    //      ...item,
    //      chanName: item.roleId.toString(),
    //     };
    //   });
    //   if (!formParams.userId) {
    //   formParams.userId = 0;
    // } else {
    //   formParams.userId = parseInt(formParams.userId);
    // }
    // const param = { ...formParams, ...res };
    // console.log(param);
    // await getPlayerPageList(param);
    // }
    // console.log("data=======================",data);
    return data;
  };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }

  function reloadTable() {
    actionRef.value.reload();
  }

  function handleSubmit(values: Recordable) {
    console.log('handle', values);
    // values.userId = Number(values.userId);
    let tempParams = unref(formParams);
    Object.assign(tempParams, values);
    reloadTable();
  }

  function handleReset(values: Recordable) {
    console.log(values);
  }

  onMounted(async () => {
    let areaVal = await getAllArea();
    optionsRef.value = areaVal.map((value) => {
      return {
        label: value.serverName,
        value: value.serverId,
      };
    });
    const games = await getGameAll();
    gameOptions.value = games.map((game) => {
      return {
        label: game.gameName,
        value: game.gameId,
      };
    });
    let firstData = unref(gameOptions)[0];
    setFieldsValue({ gameKey: firstData.value });
    unref(formParams).gameKey = firstData.value;
    reloadTable();
    nextTick(() => {
      if(actionRef && actionRef.value && actionRef.value.columnSetRef ) {
            let obj =  actionRef.value.columnSetRef;
            let arr = obj.defaultCheckList;
            arr.splice(arr.indexOf("onlineTime"),1);
            obj.onChange(arr);
          }
        });
  });
</script>

<style lang="less" scoped></style>
